﻿#pragma once

#include "lzma/LzmaLib.h"

class Compress
{
public:
	////压缩Gzip文件
	//static unsigned char * GzipCompress(unsigned char * bytes)
	//{
	//	unsigned char * result = 0;
	//	using (MemoryStream inStream = new MemoryStream(bytes))
	//	{
	//		using (MemoryStream outStream = new MemoryStream())
	//		{
	//			using (GZipOutputStream gZipOutputStream = new GZipOutputStream(outStream))
	//			{
	//				gZipOutputStream.Write(bytes, 0, bytes.Length);
	//			}

	//			result = outStream.ToArray();
	//		}
	//	}

	//	return result;
	//}

	////解压Gzip文件
	//static unsigned char * GzipDecompress(unsigned char * bytes)
	//{
	//	unsigned char * result;
	//	using (MemoryStream inStream = new MemoryStream(bytes))
	//	{
	//		using (GZipInputStream gZipInputStream = new GZipInputStream(inStream))
	//		{
	//			using (MemoryStream outStream = new MemoryStream())
	//			{
	//				unsigned char * array = new byte[4096];
	//				int num;
	//				while ((num = gZipInputStream.Read(array, 0, array.Length)) != 0)
	//				{
	//					outStream.Write(array, 0, num);
	//				}
	//				result = outStream.ToArray();
	//			}
	//		}
	//	}

	//	return result;
	//}

	////压缩BZip2文件
	//static unsigned char * BZip2Compress(unsigned char * bytes)
	//{
	//	unsigned char * result;
	//	using (MemoryStream inStream = new MemoryStream(bytes))
	//	{
	//		using (MemoryStream outStream = new MemoryStream())
	//		{
	//			BZip2.Compress(inStream, outStream, true, 1);
	//			result = outStream.ToArray();
	//		}
	//	}

	//	return result;
	//}

	////解压BZip2文件
	//static unsigned char * BZip2Decompress(unsigned char * bytes)
	//{
	//	unsigned char * result;
	//	using (MemoryStream inStream = new MemoryStream(bytes))
	//	{
	//		using (MemoryStream outStream = new MemoryStream())
	//		{
	//			BZip2.Decompress(inStream, outStream, true);
	//			result = outStream.ToArray();
	//		}
	//	}

	//	return result;
	//}

	//解压LZMA文件
	static unsigned char * LZMADecompress(unsigned char * bytes, long &nLength)
	{
		size_t nSize = nLength;
		unsigned char prop[5] = {0};  
		size_t nPropSize = 5; 

		size_t nDstSize = 1;
		unsigned char *pDst = (unsigned char*)malloc(1);
		int nRet = LzmaUncompress(pDst, &nDstSize, bytes, &nSize, prop, nPropSize);
		if(nRet == SZ_ERROR_INPUT_EOF)
		{
			free(pDst);
			pDst = (unsigned char*)malloc(nDstSize);
			LzmaUncompress(pDst, &nDstSize, bytes, &nSize, prop, nPropSize);
		}

		nLength = nDstSize;
		return pDst;
	}
};